namespace C1.Win.C1FlexGrid
{
    using System;
    using System.IO;
    using System.Text;

    internal class GridSaveLoad
    {
        internal static void LoadCellText(C1FlexGridBase flex, int row, int col, string cell)
        {
            if (cell != null)
            {
                if (((cell.Length > 1) && (cell[0] == '"')) && (cell[cell.Length - 1] == '"'))
                {
                    cell = cell.Substring(1, cell.Length - 2);
                }
                if (cell.IndexOf('"') > -1)
                {
                    cell = cell.Replace("\"\"", "\"");
                }
            }
            if (col >= flex.Cols.Count)
            {
                flex.Cols.Count = col + 1;
            }
            if (row >= flex.Rows.Count)
            {
                flex.Rows.Count = row + 1;
            }
            flex[row, col] = cell;
        }

        internal static void LoadGrid(C1FlexGridBase flex, string fname, FileFormatEnum ffmt, bool fixedCells, Encoding encoding)
        {
            bool redraw = flex.Redraw;
            char ch = ',';
            switch (ffmt)
            {
                case FileFormatEnum.TextComma:
                    ch = ',';
                    break;

                case FileFormatEnum.TextTab:
                    ch = '\t';
                    break;

                case FileFormatEnum.TextCustom:
                    ch = flex.ClipSeparators[0];
                    break;

                default:
                    throw new ArgumentException(string.Format("Invalid file format: {0}.", ffmt));
            }
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(fname, encoding, true);
                if (flex.DataSource != null)
                {
                    flex.DataSource = null;
                    flex.Cols.Count = flex.Cols.Fixed;
                    flex.Rows.Count = flex.Rows.Fixed;
                }
                flex.Redraw = false;
                int row = fixedCells ? 0 : flex.Rows.Fixed;
                string str = reader.ReadLine();
                while (str != null)
                {
                    int col = fixedCells ? 0 : flex.Cols.Fixed;
                    str = str.Replace(@"\r\n", "\r\n");
                    string cell = string.Empty;
                    int startIndex = 0;
                    bool flag2 = false;
                    for (int i = 0; i < str.Length; i++)
                    {
                        if (str[i] == '"')
                        {
                            if (i == startIndex)
                            {
                                flag2 = true;
                                continue;
                            }
                            if ((i < (str.Length - 1)) && (str[i + 1] == '"'))
                            {
                                i++;
                                continue;
                            }
                            if (flag2)
                            {
                                flag2 = false;
                                continue;
                            }
                        }
                        if (!flag2 && (str[i] == ch))
                        {
                            cell = str.Substring(startIndex, i - startIndex);
                            LoadCellText(flex, row, col, cell);
                            startIndex = i + 1;
                            col++;
                        }
                    }
                    cell = str.Substring(startIndex, str.Length - startIndex);
                    LoadCellText(flex, row, col, cell);
                    str = reader.ReadLine();
                    row++;
                }
            }
            finally
            {
                flex.Redraw = redraw;
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }

        internal static void SaveGrid(C1FlexGridBase flex, string fname, FileFormatEnum ffmt, bool fixedCells, Encoding encoding)
        {
            string str;
            string str2;
            switch (ffmt)
            {
                case FileFormatEnum.TextComma:
                    str = ",";
                    str2 = "\r\n";
                    break;

                case FileFormatEnum.TextTab:
                    str = "\t";
                    str2 = "\r\n";
                    break;

                case FileFormatEnum.TextCustom:
                    str = flex.ClipSeparators[0].ToString();
                    str2 = "\r\n";
                    break;

                default:
                    throw new ArgumentException(string.Format("Invalid file format: {0}.", ffmt));
            }
            StreamWriter writer = null;
            try
            {
                writer = new StreamWriter(fname, false, encoding);
                for (int i = fixedCells ? 0 : flex.Rows.Fixed; i < flex.Rows.Count; i++)
                {
                    for (int j = fixedCells ? 0 : flex.Cols.Fixed; j < flex.Cols.Count; j++)
                    {
                        string dataDisplay = flex.GetDataDisplay(i, j);
                        if (dataDisplay == null)
                        {
                            dataDisplay = string.Empty;
                        }
                        dataDisplay = dataDisplay.Replace("\r\n", @"\r\n");
                        bool flag = dataDisplay.IndexOf("\"") > -1;
                        if (flag)
                        {
                            dataDisplay = dataDisplay.Replace("\"", "\"\"");
                        }
                        if ((flag || (dataDisplay.IndexOf(str) > -1)) || (dataDisplay.IndexOf(str2) > -1))
                        {
                            dataDisplay = "\"" + dataDisplay + "\"";
                        }
                        writer.Write(dataDisplay);
                        if (j < (flex.Cols.Count - 1))
                        {
                            writer.Write(str);
                        }
                    }
                    if (i < (flex.Rows.Count - 1))
                    {
                        writer.Write(str2);
                    }
                }
            }
            finally
            {
                if (writer != null)
                {
                    writer.Flush();
                    writer.Close();
                }
            }
        }
    }
}

